package me.sdevil507.biz.fruit.repository;

import me.sdevil507.biz.fruit.po.FruitOrgRelPo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface FruitOrgRelRepository extends JpaRepository<FruitOrgRelPo, Long>, JpaSpecificationExecutor<FruitOrgRelPo>, QuerydslPredicateExecutor<FruitOrgRelPo> {

    void deleteFruitOrgRelPosByFruitIdAndOrgIdAndStatus(long fruitId, long orgId, String status);

    void deleteFruitOrgRelPosByFruitIdAndStatus(long fruitId, String status);

    void deleteFruitOrgRelPosByFruitIdAndAuthUserId(long fruitId, long userId);

    FruitOrgRelPo findFruitOrgRelPoByFruitIdAndOrgIdAndStatus(long fruitId, long orgId, String status);

    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update FruitOrgRelPo a set a.authUserId=:newUserId where a.authUserId=:oldUserId")
    void updateAuthorId(@Param("oldUserId") Long oldUserId, @Param("newUserId") Long newUserId);
}
